<?

if ($DATA['component']['id']) {

	// list editor
	
	$_id_component	= $DATA['component']['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value
	
	$_crit = "id_data=$_id_data AND id_parent=$_id_component";
	
	
	$DATA['objects'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb('data'), 'where' => "$_crit",	'order' => 'id_index', 'index' => 'id' )));
	
// tasks handling

	if ($a = (int)param('delete')) {

		dataset('killmeta', "id_data=$_id_meta AND id_parent=$a");
		dataset('db_delete', tb('data'), "$_crit AND id=$a");
	
		exit;
	}
	
	if ($a = (int)param('show')) {

		dataset('db_update', tb('data'), "attr = attr | ".AA_VISIBLE, "$_crit AND (id=$a)");
	
		exit;
	}
	
	if ($a = (int)param('hide')) {
		
		dataset('db_update', tb('data'), "attr = attr & ~".AA_VISIBLE, "$_crit AND (id=$a)");
	
		exit;
	}
	
	
if ($a = strtolower(trim(param('sort')))) {
	
	switch($a) {
	
		case 'manual':	// ajax request, no data should be returned

			$t = 1;
			
			parse_str(postdata('order'), $temp);
			
			if (is_array($temp = $temp['objects']) && sizeof($temp)) {

				foreach($temp as $id) {
					if ($id = (int)$id) {
						if ($DATA['objects'][$id]) {
							dataset('db_update', tb('data'), "id_index = ".$t++, "$_crit AND id=$id");
							unset($DATA['objects'][$id]);
						}
					}
				}
				
				if (sizeof($DATA['objects']))
					foreach($DATA['objects'] as $id => $v) 
						dataset('db_update', tb('data'), "id_index = ".$t++, "$_crit AND id=$id");
			}
			
			exit;	// ajax: no more processing needed
			break;
}
	
	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
	exit;
}
	
	



	
	switch (action(array('create_object'))) {	// if POST contains submit button name
	
	
		case 'create_object':
			
			if ($title = trim(postdata('title'))) {
	
				$max = ($m = datafetch('db_item', tb('data'), $_crit, 'MAX(id_index) as m')) ? $m['m']+1 : 1;
	
				if ($id = datafetch('db_insert', tb('data'), array('id_data' => $_id_data, 'id_parent' => $_id_component,
						'attr' => 0, 
						'name' => $title, 
						'id_index' => $max, 
					))) {
					
					dataset('db_insert', tb('data'), 
						array('id_data' => $_id_meta, 'id_parent' => $id, 'name' => 'title', 'value' => $title, 'assoc' => LANG_DEF)); 
					
					
					// redirect to edit page
					header("Location: "._transformURL($CURRENT->url, array('add_param' => "edit:$id")));
					exit;
				}
					// !!! else error on insert - handle it
			}
			
			break;
	
	}


	return false;
} else exit;

?>